package controller

import (
	"github.com/gin-gonic/gin"
	"web/app"
	"web/app/dao"
	"web/app/enum"
	"web/app/mode"
	"xutil/xStr"
)

type c_apply struct {
	dao dao.APPLY
}

func (c2 c_apply) Add(c *gin.Context) {
	var a mode.Apply
	err := c.ShouldBind(&a)
	if err != nil || len(a.Name) == 0 {
		app.Response(c, 400, "参数错误")
		return
	}
	if a.Mold <= 0 && a.Mold > 2 {
		a.Mold = enum.APPLY_MOLD_KouShi
	}
	if a.Login <= 0 && a.Login > 3 {
		a.Login = enum.APPLY_Login_MianFei
	}
	if a.Bin <= 0 && a.Bin > 3 {
		a.Bin = enum.APPLY_Bin_NoBin
	}

	err = c2.dao.ADD(&a)
	if err != nil {
		app.Response(c, 400, "添加失败")
		return
	}
	app.Response(c, 200, "添加成功")
}

func (c2 c_apply) Get(c *gin.Context) {
	aid := c.Query("aid")
	err, a := c2.dao.Get(xStr.Str2int(aid))
	if err != nil || a.Aid == 0 {
		app.Response(c, 400, "软件不存在")
		return
	}
	if a.Login != enum.APPLY_Login_MianFei {
		app.Response(c, 400, "收费软件不可直接获取信息")
		return
	}
	app.Response_data(c, 200, "查询成功", a)
}

func (c2 c_apply) GetAll(c *gin.Context) {
	var a mode.Apply
	err := c.ShouldBind(&a)
	if err != nil {
		app.Response(c, 400, "参数错误")
		return
	}

	err, list := c2.dao.GetAll(&a)
	if err != nil {
		app.Response(c, 400, "查询失败")
		return
	}
	app.Response_list(c, 200, "查询成功", list)
}

func (c2 c_apply) Edit(c *gin.Context) {
	var a mode.Apply
	err := c.ShouldBind(&a)
	if err != nil || a.Aid == 0 {
		app.Response(c, 400, "软件不存在")
		return
	}

	err = c2.dao.Edit(&a)
	if err != nil {
		app.Response(c, 400, "修改失败")
		return
	}
	app.Response(c, 200, "修改成功")
}

type i_apply interface {
	Add(c *gin.Context)
	Get(c *gin.Context)
	GetAll(c *gin.Context)
	Edit(c *gin.Context)
}
